package com.sc.study.clazz.service.impl;

import java.util.List;

import com.sc.common.utils.DateUtils;
import com.sc.study.clazz.domain.ScClazzSubject;
import com.sc.study.clazz.mapper.ScClazzMapper;
import com.sc.study.clazz.mapper.ScClazzSubjectMapper;
import com.sc.study.foundation.mapper.ScSubjectMapper;
import com.sc.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sc.study.clazz.mapper.ScClazzSubjectTeacherMapper;
import com.sc.study.clazz.domain.ScClazzSubjectTeacher;
import com.sc.study.clazz.service.IScClazzSubjectTeacherService;

/**
 * 班级科目授权老师Service业务层处理
 *
 * @author sc
 * @date 2022-02-27
 */
@Service
public class ScClazzSubjectTeacherServiceImpl implements IScClazzSubjectTeacherService {
    @Autowired
    private ScClazzSubjectTeacherMapper scClazzSubjectTeacherMapper;
    @Autowired
    private ScSubjectMapper scSubjectMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ScClazzMapper scClazzMapper;
    @Autowired
    private ScClazzSubjectMapper scClazzSubjectMapper;


    /**
     * 查询班级科目授权老师包含科目
     *
     * @param id 班级科目授权老师主键
     * @return 班级科目授权老师
     */
    @Override
    public ScClazzSubjectTeacher selectScClazzSubjectTeacherById(Long id) {
        return scClazzSubjectTeacherMapper.selectScClazzSubjectTeacherById(id);
    }

    /**
     * 查询班级科目授权老师列表包含班级、科目、老师
     *
     * @param scClazzSubjectTeacher 班级科目授权老师
     * @return 班级科目授权老师
     */
    @Override
    public List<ScClazzSubjectTeacher> selectScClazzSubjectTeacherList(ScClazzSubjectTeacher scClazzSubjectTeacher) {
        List<ScClazzSubjectTeacher> scClazzSubjectTeacherList = scClazzSubjectTeacherMapper.selectScClazzSubjectTeacherList(scClazzSubjectTeacher);
        scClazzSubjectTeacherList.forEach(item -> {
            ScClazzSubject scClazzSubject = scClazzSubjectMapper.selectScClazzSubjectById(item.getClazzsubid());
            // 设置科目
            item.setScSubject(scSubjectMapper.selectScSubjectBySubid(scClazzSubject.getSubid()));
            // 设置班级
            item.setScClazz(scClazzMapper.selectScClazzById(scClazzSubject.getClazzid()));
            // 设置老师
            item.setSysUser(sysUserMapper.selectUserById(item.getUserid()));
        });
        return scClazzSubjectTeacherList;
    }

    /**
     * 新增班级科目授权老师
     *
     * @param scClazzSubjectTeacher 班级科目授权老师
     * @return 结果
     */
    @Override
    public int insertScClazzSubjectTeacher(ScClazzSubjectTeacher scClazzSubjectTeacher) {
        scClazzSubjectTeacher.setStarttime(DateUtils.getLocalDateTime());
        ScClazzSubject scClazzSubject=new ScClazzSubject();
        scClazzSubject.setClazzid(scClazzSubjectTeacher.getClazzId());
        scClazzSubject.setSubid(scClazzSubjectTeacher.getSubId());
        // 根据班级编号和科目编号找到唯一的班级科目
        ScClazzSubject clazzSubject = scClazzSubjectMapper.selectScClazzSubjectList(scClazzSubject).get(0);

        // 查询是否已经有老师代课
        ScClazzSubjectTeacher clazzSubjectTeacher=new ScClazzSubjectTeacher();
        clazzSubjectTeacher.setClazzsubid(clazzSubject.getId());
        // 根据课程班级课程id查询班级课程老师表中没有结束时间的
        List<ScClazzSubjectTeacher> subjectTeacherList = scClazzSubjectTeacherMapper.selectScClazzSubjectTeacherListExcludeEndTime(clazzSubjectTeacher);
        // 如果有老师代课就将该老师结束时间设置为当前时间

        if(subjectTeacherList.size()>0){
            ScClazzSubjectTeacher clazzSubjectTeacher1 = subjectTeacherList.get(0);
            // 设置结束时间
            clazzSubjectTeacher1.setEndtime(DateUtils.getLocalDateTime());

            // 修改之前授课老师记录
            scClazzSubjectTeacherMapper.updateScClazzSubjectTeacher(clazzSubjectTeacher1);
        }

        scClazzSubjectTeacher.setClazzsubid(clazzSubject.getId());

        return scClazzSubjectTeacherMapper.insertScClazzSubjectTeacher(scClazzSubjectTeacher);
    }

    /**
     * 修改班级科目授权老师
     *
     * @param scClazzSubjectTeacher 班级科目授权老师
     * @return 结果
     */
    @Override
    public int updateScClazzSubjectTeacher(ScClazzSubjectTeacher scClazzSubjectTeacher) {
        return scClazzSubjectTeacherMapper.updateScClazzSubjectTeacher(scClazzSubjectTeacher);
    }

    /**
     * 批量删除班级科目授权老师
     *
     * @param ids 需要删除的班级科目授权老师主键
     * @return 结果
     */
    @Override
    public int deleteScClazzSubjectTeacherByIds(Long[] ids) {
        return scClazzSubjectTeacherMapper.deleteScClazzSubjectTeacherByIds(ids);
    }

    /**
     * 删除班级科目授权老师信息
     *
     * @param id 班级科目授权老师主键
     * @return 结果
     */
    @Override
    public int deleteScClazzSubjectTeacherById(Long id) {
        return scClazzSubjectTeacherMapper.deleteScClazzSubjectTeacherById(id);
    }
}
